package com.procurement.bidding.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.procurement.bidding.domain.PcmBiddingDemandMain;
import com.procurement.bidding.mapper.PcmBiddingDemandMainMapper;
import com.procurement.bidding.service.IPcmBiddingDemandMainService;
import com.procurement.bidding.utils.GetUser;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.RenotePcmService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 招标需求主Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class PcmBiddingDemandMainServiceImpl implements IPcmBiddingDemandMainService
{
    @Resource
    private PcmBiddingDemandMainMapper pdmm;

    @Resource
    private RenotePcmService rps;

    @Override
    public List<PcmBiddingDemandMain> showAllLj(String demandNumber) {
        return pdmm.showAllLj(demandNumber);
    }

    @Override
    public List<Map<String, Object>> showAllMx(Map<String, Object> map) {
        return pdmm.showAllMx(map);
    }

    @Override
    public List<Map<String, Object>> findAllByProjectID(Integer projectID) {
        return pdmm.findAllByProjectID(projectID);
    }

    @Override
    @Transactional
    public Integer insertDemandMain(Map<String, Object> map) {
        map.put("demandNumber",rps.scbm("xj", SecurityConstants.INNER));
        map.put("submitBidDemandPerson", GetUser.getLoggedInUserId());
        map.put("submitBidDemandTime",new Date());
        map.put("bidDemandUpdateTime",new Date());
        List<Integer> listDetailIds = (List<Integer>) map.get("listDetailIds");
        Integer addMain = pdmm.insertDemandMain(map);
        Integer projectID = Integer.valueOf(map.get("projectID").toString());
        Integer addZj = pdmm.addDemandDetail(projectID, listDetailIds);
        Integer xgStatus = pdmm.updateDemandLineStatus(listDetailIds);
        return addMain+addZj+xgStatus;
    }

    @Override
    @Transactional
    public Integer updateDemandMain(Map<String, Object> map) {
        map.put("bidDemandUpdateTime",new Date());
        Integer xgTime = pdmm.updateTime(map);
        Integer projectID = Integer.valueOf(map.get("projectID").toString());
        List<Map<String,Object>> pcmApplicationDetailList = (List<Map<String, Object>>) map.get("pcmApplicationDetailList");
        //需要删除的明细数据
        List<Integer> delDetailIds = new ArrayList<>();
        //需要添加的数据
        List<Integer> addDetailIds = new ArrayList<>();
        for (Map<String, Object> detail : pcmApplicationDetailList) {
            Integer detailId = (Integer) detail.get("detail_id");
            if (detail.containsKey("demand_detail_id") && detail.get("demand_detail_id").equals(0)) {
                addDetailIds.add(detailId);
            }else {
                delDetailIds.add(Integer.valueOf(detail.get("demand_detail_id").toString()));
            }
        }
        Integer lala = 0;
        if (addDetailIds!=null&&addDetailIds.size()>0){
            lala += pdmm.updateDemandLineStatus(addDetailIds);
            lala += pdmm.addDemandDetail(projectID, addDetailIds);
        }
        if (delDetailIds!=null&&delDetailIds.size()>0){
            lala += pdmm.delDemandDetail(delDetailIds, projectID);
        }
        return xgTime+lala;
    }

    @Override
    public Integer delDemandMain(List<Integer> projectIDs) {
        return pdmm.delDemandMain(projectIDs);
    }


}
